
import 'package:valovault_reminder/pages/base/val_base_ui_page.dart';
import 'package:valovault_reminder/common/config/val_assets_name.dart';
import 'package:flutter/material.dart';

typedef OnTapTabBar = void Function(int index);

abstract class ValCommonNavigationPageState<T extends StatefulWidget>
    extends ValBaseUIPageState<T> {
  @override
  bool get isAppBar => false;

  PageController? pageController; // page左右切换界面
  int pageIndex = 0; // 当前页面下标
  Color? tabBarColor = const Color(0xFF171723); // tabBar背景色
  Color? themeColor; // 主题色
  Color? unSelectColor; // 未选中颜色
  bool isAnimate = false; // 是否添加动画
  bool isScroll = false; // 是否启用手势滚动
  BottomNavigationBarType type = BottomNavigationBarType.fixed; //底部导航显示样式
  OnTapTabBar? onTapTabBar; //点击tab

  //界面数组 必须重写
  @protected
  List<Widget> getWidgets();

  //底部item数组 必须重写
  @protected
  List<BottomNavigationBarItem> getBottomNavigationBarItems(
      BuildContext context);

  @override
  void initState() {
    resizeToAvoidBottomInset = false;
    super.initState();
    pageController = PageController(initialPage: this.pageIndex);
  }

  //初始化baritem方法
  BottomNavigationBarItem barItem({required IconData iconData, String title = ''}) {
    return BottomNavigationBarItem(
      icon: Icon(iconData, color: unSelectColor), //未选中图标
      activeIcon: Icon(iconData, color: themeColor), //选中图标
      label: title,
    );
  }

  BottomNavigationBarItem barItemWithImage({required String imageName, required String selectedImageName, String title = '', double size = 24.0}) {
    return BottomNavigationBarItem(
      icon: Image.asset(
        '${ValAssetsName.ASSETS_IMAGES}$imageName',
        width: size,
        height: size,
      ), //未选中图标
      activeIcon: Image.asset(
        '${ValAssetsName.ASSETS_IMAGES}$selectedImageName',
        width: size,
        height: size,
      ), //选中图标
      label: title,
    );
  }


  @override
  Widget buildBody(BuildContext context) {
    return PageView(
      controller: pageController, //页面控制器
      onPageChanged: onPageChanged, //切换方法
      physics: isScroll ? null : const NeverScrollableScrollPhysics(), //禁止滑动
      children: getWidgets(), //添加页面参数
    );
  }

  @override
  Widget buildNavigationBar(BuildContext context) {
    return BottomNavigationBar(
      items: getBottomNavigationBarItems(context),
      //触控参数
      onTap: onTap,
      //点击事件
      type: type,
      currentIndex: pageIndex,
      backgroundColor: tabBarColor,
    );
  }

  void onTap(int index) {
    if (isAnimate) {
      //动画跳转
      pageController?.animateToPage(index,
          duration: const Duration(milliseconds: 200), curve: Curves.ease);
    } else {
      //无动画跳转
      pageController?.jumpToPage(index);
    }

    if (onTapTabBar != null) {
      onTapTabBar!(index);
    }
  }

  void onPageChanged(int page) {
    if (mounted) {
      setState(() {
        this.pageIndex = page;
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
  }
}
